import Pool from './base/pool'
import Music from './runtime/music'

let instance

const Score_IMG_SRC = 'images/score.png'
const { windowWidth, windowHeight, statusBarHeight} = wx.getSystemInfoSync()
// let menu_rect = wx.getMenuButtonBoundingClientRect();

/**
 * 全局状态管理器
 */
export default class DataBus {
  constructor() {
    if ( instance )
      return instance

    instance = this

    // 全局位置
    this.start_x = 0 //初始x
    this.start_y = 0 //初始y
    this.c_height = 30 //下落区间
    this.line_num = 20 //横排数量
    this.diamondSize = canvas.width / this.line_num //单个方块宽高 
    this.actionSize = 3 //底部操作宽高

    // let menu_rect = wx.getMenuButtonBoundingClientRect();
    // this.score_rect = [windowWidth - menu_rect.right, menu_rect.top, menu_rect.width, menu_rect.height]
    
    //正在运行的方块集合
    // this.run_diamonds = []
    // this.floor_diamonds = []
    // this.floor_diamonds_judge = [] //将方块数据拼接为字符串判断
    // this.score = 0
    // this.gameOver = false
    //添加暂停测试
    // this.pause = false
    this.reset()

    this.pool = new Pool()
    this.music = new Music()

    this.mutiGameType = true;//false:普通模式 默认 true:挑战模式

    
    //道具集 
    // 1.加速器(a_run操作控制加速)
    // 2.计时器(clock倒计时未消除即结束) 
    // 3.混乱控制(s_horizontal左右不受控制)
    // 4.炸弹(bomb随机消除已存在的一行)
    // 5.蒙面(eye_miss运行中不可见)
    // 6.下落速度加快(a_down) 
    // 7.钻石(diamond获得额外钻石) 
    // 8.操作锁定(lock操作不允许执行: 左)
    // 9.操作锁定(lock操作不允许执行: 旋转)
    // 10.操作锁定(lock操作不允许执行: 右)
    this.prop_item = {}

    this.propState = 0;//没有中道具状态

    this.time_in = false //是否在倒计时
    this.time_end = Date.now() + 1000 * 60 *1 //1分钟

    this.resetFrame()

    this.last_max_score()
  }
  //适配区间
  resetFrame(){

    // //重置初始Y,适配
    this.start_y = Math.floor(statusBarHeight / this.diamondSize)

    let all_y = Math.floor(windowHeight / this.diamondSize)
    // 适配高度 - 底部空出2格的高度，操作和内容栏也空出1格
    this.c_height = Math.floor(all_y - this.actionSize - 3)

    this.action_y = all_y - 5;
  }
  // 向左
  leftAction(){
    for (let i = 0; i < this.run_diamonds.length; i++) {
      let diamond = this.run_diamonds[i];
      diamond.turn(true);
    }
  }
  // 旋转
  refreshAction(){
    for (let i = 0; i < this.run_diamonds.length; i++) {
      let diamond = this.run_diamonds[i];
      diamond.transform();
    }
  }
  // 向右
  rightAction(){
    for (let i = 0; i < this.run_diamonds.length; i++) {
      let diamond = this.run_diamonds[i];
      diamond.turn(false);
    }
  }

  // 是否可放置方块item
  isCanLocationItem(itemRects){
    let isIn = this.floor_diamonds_judge.includes(itemRects.join(","))
    if (isIn) {
      return false;
    }
    return true;
  }
  // 存储方块
  receiveDiamond(diamond) {
    let y_s = []
    for (let i = 0; i < diamond.length; i++) {
      let item = diamond[i];
      item.x = Math.round(item.x);
      item.y = Math.round(item.y);
      item.w = Math.round(item.w);
      item.h = Math.round(item.h);
      // item.location = 0;
      this.floor_diamonds.push(item)
      //将要触发结算，保留y轴数组，去重
      if (!y_s.includes(item.y)) {
        y_s.push(item.y)
      }
    }
    this.settle(y_s)
  }
  // 触发结算 - 以y轴作为结算 - 传入的是变更的y轴数组
  settle(y_s){

    // 游戏结束
    if (y_s.includes(this.start_y)){
      
      this.gameOver = true
      this.saveScore_max()
      return;
    }
    let bomb = false
    if (this.propState === 4) //4.炸弹
    {
      bomb = true
      this.propState = 0
    }

    for (let t = 0; t < y_s.length; t++) {
      let y = y_s[t];
      let item_stand = []
      for (let i = 0, len = this.floor_diamonds.length; i < len; i++) {
        let item = this.floor_diamonds[i];
        if (item.y === y) {
          item_stand.push(i);
        }
      }
      if (item_stand.length >= this.line_num || bomb === true) {
        
        //数量等于整行，消除；同时上面的数据方格向下一行
        for (let len = item_stand.length, i = len-1; i >= 0; i--){
          let t = item_stand[i];
          this.resaveItem(this.floor_diamonds[t]);//回收方块
          this.floor_diamonds.splice(t,1);//从底部数组中移除
        }
        // 同时上面的数据方格向下一行
        for (let i = 0, len = this.floor_diamonds.length; i < len; i++){
          let item = this.floor_diamonds[i];
          if (item.y < y) {
            item.y += 1;
          }
        }
        this.music.playShoot() //播放消除声音
        this.score += 1 //分数加
        if (this.time_in) //2.计时器 如果是倒计时，把倒计时状态去除
        { this.time_in = false }
      }
    }
    //将所有方块数据转换为字符串存储校验
    let floor_store = []
    for (let len = this.floor_diamonds.length, i = len -1; i >= 0; i--)     {
      let item = this.floor_diamonds[i];
      floor_store.push([item.x,item.y,item.w,item.h].join(","));
    }
    this.floor_diamonds_judge = floor_store;
  }

  //回收方块
  resaveItem(item){
    this.pool.recover('DItem', item)
  }

  //回收道具
  resaveProp(prop_item) {
    this.pool.recover('DPropItem', prop_item)
  }

  reset() {
    this.score      = 0
    this.gameOver = false
    this.pause = false
    this.run_diamonds = []
    this.floor_diamonds = []
    this.floor_diamonds_judge = [] //将方块数据拼接为字符串判断
    this.propState = 0;//没有中道具状态
    this.time_in = false //是否在倒计时
    if (this.prop_item !== undefined) {//如果存在赋值，把道具展示去掉
      this.prop_item.visible = false
    }
  }

  last_max_score(){

    let openData = wx.getOpenDataContext()
    openData.postMessage({ "last_score": true })
  }
  
  saveScore_max(){

    let openData = wx.getOpenDataContext()
    openData.postMessage({ "score": this.score })
  }

}
